<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/alllayui/layui/css/layui.css}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div style="height: 10px;"></div>
<div class="layui-container" > 
 <div class="layui-form searchTable">
    时间：
     <div class="layui-inline">  
      <div class="layui-input-inline">
        <input type="text" name="publishTimeStart" id="publishTimeStart" lay-verify="date" placeholder="开始时间" autocomplete="off" class="layui-input datePicker">
      </div>
    </div>
    -
     <div class="layui-inline">  
      <div class="layui-input-inline">
        <input type="text" name="publishTimeEnd" id="publishTimeEnd" lay-verify="date" placeholder="结束时间" autocomplete="off" class="layui-input datePicker">
      </div>
    </div>
       状态： 
     <div class="layui-input-inline">
      <select name="nstate" id="nstate" >
        <option value="">请选择状态</option> 
        <option value="0">起草</option>
        <option value="1">发表</option>
      </select>
    </div>
     
    <button id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
</div>
 
<table class="layui-hide" id="Orgnews" lay-filter="Orgnews"></table>
 </div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="confirmSelect">确定</button>
    </div>
</script>
<script type="text/javascript">
    var btnCls = ['layui-btn-warm','layui-btn-danger','layui-btn-disabled','layui-btn-normal'];
	function dict(rowD,str){ 
		var ret = "";
		var btnIndex = 0;
		if(str=="nstate"){
			if(rowD.nstate==0){
				btnIndex = 0;
				ret = "起草";
			} 
			if(rowD.nstate==1){
				btnIndex = 1;
				ret = "发表";
			} 
		}
		 
		if(str=="newsType"){
			if(rowD.newsType==0){
				btnIndex = 0;
				ret = "内部使用";
			} 
			if(rowD.newsType==1){
				btnIndex = 1;
				ret = "外部使用";
			} 
		}
		if(str=="allowComment"){
			if(rowD.allowComment==0){
				btnIndex = 0;
				ret = "开启评论";
			} 
			if(rowD.allowComment==1){
				btnIndex = 1;
				ret = "关闭评论";
			} 
		}
		 
		var btn = "<a class=\"layui-btn layui-btn-xs layui-btn-radius "+ btnCls[btnIndex]+ "\">"+ ret +"</a>"; 
		return btn;
	}
</script>


<script th:src="@{/alllayui/layui/layui.js}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['jquery','table','form','laydate'], function(){
    	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    	  var table = layui.table; 
          var form = layui.form
          ,layer = layui.layer
          ,$ = layui.$ ;
     	    var laydate = layui.laydate;
          
          table.render({
              elem: '#Orgnews'
              ,url:'/orgnews/list'
              ,method: 'post'
              ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
              ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                  title: '提示'
                  ,layEvent: 'LAYTABLE_TIPS'
                  ,icon: 'layui-icon-tips'
              }]
              ,title: 'Orgnews'
              ,cols: [
                  [
                  {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title:'ID',  align:'center' }
                ,{field: 'newsType', title:'新闻类型',  align:'center' , templet: function(d){return dict(d,'newsType');} }
                ,{field: 'ntitle', title:'新闻标题',  align:'center' }
                ,{field: 'ncontent', title:'新闻内容',  align:'center' }
                ,{field: 'publishTime', title:'发表时间',  align:'center' } 
                ,{field: 'nstate', title:'状态',  align:'center', templet: function(d){return dict(d,'nstate');} }
                ,{field: 'allowComment', title:'是否开启评论',  align:'center', templet: function(d){return dict(d,'allowComment');} } 
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
              ]
              ]
              ,page: true
              ,request: {
                  pageName: 'currentPageNum' //页码的参数名称，默认：page
                  ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
              }
              ,parseData: function(res){ //res 即为原始返回的数据
                  return {
                      "code": 0, //解析接口状态
                      "msg": "", //解析提示文本
                      "count": res.totalRecords, //解析数据长度
                      "data": res.recordList //解析数据列表
                  };
              }

          });

          
          //同时绑定多个
          lay('.datePicker').each(function(){
              laydate.render({
                  elem: this
                  ,trigger: 'click'
                  ,type: 'datetime'
              });
          });

          var $ = layui.$, active = {
              reload: function(){
                  var publishTimeStart  = $('#publishTimeStart');
                  var publishTimeEnd = $('#publishTimeEnd');
                  var nstate$_ = $('#nstate');
                  var queryWhere = {};
                  if(publishTimeStart.val()!=""  && publishTimeEnd.val()!="" ){
                  	queryWhere['publishTimeStart'] = publishTimeStart.val();
                  	queryWhere['publishTimeEnd'] = publishTimeEnd.val();
                  } 
                  if(nstate$_.val()!=""){
                  	queryWhere['nstate'] = nstate$_.val();
                  } 
                  //执行重载
                  table.reload('Orgnews', {
                      page: {
                          curr: 1 //重新从第 1 页开始
                      }
                      , method: 'POST'
                      ,where: queryWhere
                      ,done: function(res, curr, count){
                      	this.where = {};
                      }
                  }, 'data');
              }
          };

   

          $('.searchTable .layui-btn').on('click', function(){
              var type = $(this).data('type');
              active[type] ? active[type].call(this) : '';
          });
          
          
        //头工具栏事件
        table.on('toolbar(Orgnews)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'confirmSelect':
                {
                    var data = checkStatus.data;  //获取选中行数据
                    //layer.alert(JSON.stringify(data)); 
                    parent.setOrgnewsAry(data);
                    parent.layer.close(index);
                }
                    break;
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
        //监听行工具事件
        table.on('tool(Orgnews)', function(obj){
            var data = obj.data;
        });

    });


</script>

</body>
</html>